﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;
using System.Data;
using Telerik.Web.UI;


namespace HCFramework.User
{
    public partial class UserManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // An cac control chua dung den
                PanelAddAndEdit.Visible = false;
                PanelGrid.Visible = true;
            }
        }
        protected void RadGridMain_PreRender(object sender, EventArgs e)
        {
            LoadGridData();
        }
        public void LoadGridData()
        {
            HCDataContext db = new HCDataContext();
            var result = from a in db.Users
                         orderby a.DateCreated descending
                         select new
                         {
                             a.UserID,
                             a.DateCreated,
                             a.Email,
                             Name = a.FirstName + " " + a.LastName,
                             a.IsDeleted,
                             a.LastActivityDate,
                             a.LastIPAddress,
                             a.LastModifiedOnDate,
                             a.Password,
                             a.Username,
                         };
            DataTable dt = M.Common.LINQToDataTable(result);
            dt.Columns.Add("RoleName");
            foreach (DataRow row in dt.Rows)
            {
                int userID = int.Parse(row["UserID"].ToString());
                row["RoleName"] = C.UserController.GetRoleName(userID);
                row.AcceptChanges();
            }
            RadGridMain.DataSource = dt;
            RadGridMain.DataBind();
        }
        protected void RadGridMain_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Them")
            {
                PanelAddAndEdit.Visible = true;
                PanelGrid.Visible = false;
                ButtonAdd.Visible = true;
                ButtonUpdate.Visible = false;
            }
            if (e.CommandName == "Xoa")
            {
                GridDataItem item = (GridDataItem)e.Item;
                C.UserController.Delete(int.Parse(item["UserID"].Text));
            }
            if (e.CommandName == "Sua")
            {
                GridDataItem item = (GridDataItem)e.Item;
                LabelID.Text = item["UserID"].Text;
                BindEditForm();

                PanelAddAndEdit.Visible = true;
                PanelGrid.Visible = false;
                ButtonAdd.Visible = false;
                ButtonUpdate.Visible = true;
            }
        }
        private void BindEditForm()
        {
            int id = int.Parse(LabelID.Text);
            API.LINQ.User u = C.UserController.GetByID(id);
            TextBoxEmail.Text = u.Email;
            TextBoxFirstName.Text = u.FirstName;
            TextBoxLastName.Text = u.LastName;
            TextBoxPassword.Text = u.Password;
            TextBoxUsername.Text = u.Username;
        }
        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            PanelAddAndEdit.Visible = false;
            PanelGrid.Visible = true;
        }
        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            // Insert a record
            API.LINQ.User u = new API.LINQ.User();
            u.Email = TextBoxEmail.Text;
            u.FirstName = TextBoxFirstName.Text;
            u.LastName = TextBoxLastName.Text;
            u.Password = C.UserController.EncryptPassword(TextBoxPassword.Text);
            u.Username = TextBoxUsername.Text;
            u.IsDeleted = false;
            C.UserController.Insert(u);

            // Return to grid
            PanelAddAndEdit.Visible = false;
            PanelGrid.Visible = true;
        }
        protected void ButtonUpdate_Click(object sender, EventArgs e)
        {
            // Update
            int id = int.Parse(LabelID.Text);
            HCDataContext db = new HCDataContext();
            API.LINQ.User u = C.UserController.GetByID(id, db);
            u.Email = TextBoxEmail.Text;
            u.FirstName = TextBoxFirstName.Text;
            u.LastName = TextBoxLastName.Text;
            if (TextBoxPassword.Text != "") u.Password = C.UserController.EncryptPassword(TextBoxPassword.Text);
            u.Username = TextBoxUsername.Text;
            C.UserController.Update(u, db);

            // Return to grid
            PanelAddAndEdit.Visible = false;
            PanelGrid.Visible = true;
        }

        protected void LinkButtonAddRole_Click(object sender, EventArgs e)
        {
            foreach (GridItem gridItem in RadGridMain.SelectedItems)
            {
                GridDataItem dataItem = (GridDataItem)gridItem;
                int userId = int.Parse(dataItem["UserID"].Text);
                int roleId = int.Parse(ChooseRole1.DropDownList1.SelectedValue);

                UserRole userRole = new UserRole();
                userRole.RoleID = roleId;
                userRole.UserID = userId;
                C.UserRoleController.Insert(userRole);
            }
        }

        protected void LinkButtonDeleteRole_Click(object sender, EventArgs e)
        {
            foreach (GridItem gridItem in RadGridMain.SelectedItems)
            {
                GridDataItem dataItem = (GridDataItem)gridItem;
                int userId = int.Parse(dataItem["UserID"].Text);
                int roleId = int.Parse(ChooseRole1.DropDownList1.SelectedValue);

                UserRole userRole = C.UserRoleController.Get(userId, roleId);
                if (userRole != null) C.UserRoleController.Delete(userRole.UserRoleID);
            }
        }
    }
}